/**
 * MessageMapUtil.java
 * Product:BaseLayer
 * Version:1.0
 * Copyright 2009 by DNE
 * All Rights Reserved.
 */
package com.site.util;

import java.util.HashMap;

/**
 * 资源文件Message管理
 * 
 * @author chad 2009-1-12
 */
public class MessageMapUtil extends HashMap<Object, Object> implements
		MessageMap {

	private static final long serialVersionUID = 1L;
	/** 语言种类 */
	private String _lang;

	/**
	 * @param _lang
	 */
	public MessageMapUtil(String _lang) {
		super();
		this._lang = _lang;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dne.framework.common.MessageMap#getLanguage()
	 */
	public String getLanguage() {
		// TODO Auto-generated method stub
		return _lang;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dne.framework.common.MessageMap#getMessage(java.lang.String)
	 */
	public Message getMessage(String key) {
		// TODO Auto-generated method stub
		Message message = (Message) get(key);
		if (message == null) {
			message = new Message();
			message.set_dspMessage(key);
			message.set_key("Unknown");
			message.set_type(getMessageOrValue("SYS_MSG_00000"));
			return message;
		}
		return message;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dne.framework.common.MessageMap#getMessage(java.lang.String,
	 * java.lang.Object[])
	 */
	public Message getMessage(String key, Object[] param) {
		// TODO Auto-generated method stub
		Message message = getMessage(key);
		String msg = message.get_dspMessage();
		if (param == null || param.length < 1) {
			return message;
		}
		for (int i = 0; i < param.length; i++) {
			String paramStr=param[i]==null?"":param[i].toString();
			msg = msg.replace("{" + i + "}", getMessageOrValue(paramStr));
		}
		Message new_message = new Message();
		new_message.set_dspMessage(msg);
		new_message.set_key(message.get_key());
		new_message.set_type(message.get_type());
		return new_message;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.dne.framework.common.MessageMap#getMessageString(java.lang.String)
	 */
	public String getMessageString(String key) {
		// TODO Auto-generated method stub
		return getMessage(key) != null ? getMessage(key).get_dspMessage() : "";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.dne.framework.common.MessageMap#getMessageString(java.lang.String,
	 * java.lang.Object[])
	 */
	public String getMessageString(String key, Object[] param) {
		// TODO Auto-generated method stub
		return getMessage(key, param) != null ? getMessage(key, param)
				.get_dspMessage() : "";
	}

	/**
	 * 根据param参数是否MessageMap中key，不是直接返回参数本身
	 * 
	 * @param param
	 * @return
	 */
	public String getMessageOrValue(String param) {
		if (!containsKey(param)) {
			return param;
		}
		return getMessageString(param);
	}

}
